package com.david.webtest.utils;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**用于下载网页及js，css，图片等*/
public class HttpUtils {
    private static ExecutorService executorService = Executors.newFixedThreadPool(5);


    public static void downLoadImage(String url){

    }

    public static void downLoadJs(String url){

    }

    public static void downLoadCss(final String url){
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                FileWriter writer = null;
                BufferedWriter bufferedWriter = null;
                InputStreamReader inputStreamReader = null;
                BufferedReader bufferedReader = null;
                try {
                    HttpURLConnection urlConn = getConnection(url);
                    int code = urlConn.getResponseCode();

                    is = urlConn.getInputStream();

                    String name = url.substring(url.lastIndexOf("/") + 1);

                    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/blogCache/css/"+name.contains(".css") + ".html");
                    file.createNewFile();
                    writer = new FileWriter(file);
                    bufferedWriter = new BufferedWriter(writer);
                    inputStreamReader = new InputStreamReader(is);
                    bufferedReader = new BufferedReader(inputStreamReader);
                    String line = bufferedReader.readLine();
                    while(line != null){
                        bufferedWriter.write(line);
                        line = bufferedReader.readLine();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {

                        if (bufferedWriter != null) {
                            bufferedWriter.close();
                        }
                        if (writer != null) {
                            writer.close();
                        }
                        if (bufferedReader != null) {
                            bufferedReader.close();
                        }
                        if (inputStreamReader != null) {
                            inputStreamReader.close();
                        }
                        if (is != null) {
                            is.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
    }

    public static void downLoadHtml(final String url, final String name) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                FileWriter writer = null;
                BufferedWriter bufferedWriter = null;
                InputStreamReader inputStreamReader = null;
                BufferedReader bufferedReader = null;
                try {
                    HttpURLConnection urlConn = getConnection(url);
                    int code = urlConn.getResponseCode();

                    is = urlConn.getInputStream();

                    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/blogCache/"+name + ".html");
                    file.createNewFile();
                    writer = new FileWriter(file);
                    bufferedWriter = new BufferedWriter(writer);
                    inputStreamReader = new InputStreamReader(is);
                    bufferedReader = new BufferedReader(inputStreamReader);
                    String line = bufferedReader.readLine();
                    while(line != null){
                        bufferedWriter.write(line);
                        line = bufferedReader.readLine();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {

                        if (bufferedWriter != null) {
                            bufferedWriter.close();
                        }
                        if (writer != null) {
                            writer.close();
                        }
                        if (bufferedReader != null) {
                            bufferedReader.close();
                        }
                        if (inputStreamReader != null) {
                            inputStreamReader.close();
                        }
                        if (is != null) {
                            is.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
    }

    private static HttpURLConnection getConnection(String url) throws IOException {
        //创建一个url对象
        URL resurl = new URL(url);
        //设置超时时间
        HttpURLConnection urlConn = (HttpURLConnection) resurl.openConnection();
        urlConn.setConnectTimeout(5000);
        urlConn.setReadTimeout(5000);
        urlConn.setUseCaches(true);
        urlConn.connect();
        return urlConn;
    }
}
